package com.winter.sms;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.winter.mapper.NewVerificationMapper;
import com.winter.model.FVeryCodeInfo;
import com.winter.model.NewVerification;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class SmsService {

    @Autowired
    private NewVerificationMapper newVerificationMapper;


    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    static final String accessKeyId = "LTAI84ji77fUt8wm";
    static final String accessKeySecret = "0uX3KTipqwKnvNflH7rLLHRQ8mAjCf";


    public Result sendMsgCode(String phone, String type) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phone);
        request.setSignName("大金所");
        int random = (int) ((Math.random() * 9 + 1) * 100000);
        if (type.equals("1")) {
            //必填:短信模板-可在短信控制台中找到
            request.setTemplateCode("SMS_139585038");
            //可选:模板中的变量替换JSON串,如模板内容为"验证码${code}，您正在注册成为新用户，感谢您的支持！
            request.setTemplateParam("{\"code\":\"" + random + "\"}");
        } else if (type.equals("2")) {
            //必填:短信模板-可在短信控制台中找到
            request.setTemplateCode("SMS_139585037");
            //可选:模板中的变量替换JSON串,如模板内容为"验证码${code}，您正在尝试修改登录密码，请妥善保管账户信息！
            request.setTemplateParam("{\"code\":\"" + random + "\"}");
        } else if (type.equals("3")) {
            //必填:短信模板-可在短信控制台中找到
            request.setTemplateCode("SMS_139585040");
            //可选:模板中的变量替换JSON串,如模板内容为"验证码${code}，您正在尝试修改登录密码，请妥善保管账户信息！
            request.setTemplateParam("{\"code\":\"" + random + "\"}");
        }

        SendSmsResponse acsResponse = acsClient.getAcsResponse(request);
        String code = acsResponse.getCode();
        if (code != null && code.equals("OK")) {
            NewVerification f = new NewVerification();
            f.setCode(random + "");
            f.setId(UUID.randomUUID()
                        .toString());
            f.setPhoneNumber(phone);
            f.setType(type);
            Date generateTime = new Date();
            f.setGenerateTime(generateTime);
            // 十分钟
            Date date = DateUtils.addSeconds(generateTime, 10 * 60);
            f.setExpireTime(date);
            newVerificationMapper.insertSelective(f);
            return Result.success(random);
        } else {
            return Result.failure(ResultCode.FAILURE.code(), acsResponse);
        }

    }

    public static void main(String[] args) throws ClientException {
        SmsService smsService = new SmsService();
        smsService.sendMsgCode("13265235733", "1");
    }
}
